package com.example.coreshopapp.adapter;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.example.coreshopapp.R;
import com.example.coreshopapp.bean.ClassBillData;

import java.util.ArrayList;
import java.util.List;

import bean.OrderItemBean;

public class ClassBillAdapter extends RecyclerView.Adapter<ClassBillAdapter.ViewHolder> {

    private List<ClassBillData> classBillList = new ArrayList<>();

    public ClassBillAdapter(List<ClassBillData> classBillList) {
        this.classBillList = classBillList;
    }

    public void updateClassBillAdapter(List<ClassBillData> classBillList) {
        this.classBillList = classBillList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ClassBillAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_class_bill, parent, false);
        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ClassBillData item = classBillList.get(position);
        holder.timeTv.setText(item.getStarTime());
        holder.dangbanTv.setText(item.getSysUserName());
        holder.ncTv.setText(item.getInvoiceNo());
        holder.classBillNoTv.setText(item.getClassBillNo());
        holder.quantityTv.setText(item.getBillCount() +"");
        holder.storeNameTv.setText(item.getStoreName());
        holder.typeTv.setText(item.getClassOffName());
        holder.ncStatus.setText(item.getNcStatusName());
        holder.jiaobanTv.setText(item.getEndTime() != null ? (CharSequence) item.getEndTime() : "未交班");
        holder.entrynoTv.setText(position + 1 +".");

        holder.itemView.setOnClickListener(view -> {
            if (item.getNcStatus() == 3 && onItemClickListener != null) {
                onItemClickListener.onItemClick(item);
            }
        });
    }

    @Override
    public int getItemCount() {
        return classBillList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView timeTv, dangbanTv,jiaobanTv, ncTv, classBillNoTv, quantityTv, storeNameTv, typeTv,ncStatus,entrynoTv;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            timeTv = itemView.findViewById(R.id.tv_time);
            dangbanTv = itemView.findViewById(R.id.tv_place_person);
            jiaobanTv = itemView.findViewById(R.id.tv_jiaoban);
            ncTv = itemView.findViewById(R.id.tv_nc);
            classBillNoTv = itemView.findViewById(R.id.tv_classBillNo);
            quantityTv = itemView.findViewById(R.id.tv_qty);
            storeNameTv = itemView.findViewById(R.id.tv_store_name);
            typeTv = itemView.findViewById(R.id.tv_type);
            ncStatus = itemView.findViewById(R.id.tv_nc_status);
            entrynoTv = itemView.findViewById(R.id.tv_entryno);
        }
    }

    // 1.定义自定义点击事件
    public interface OnItemClickListener {
        void onItemClick(ClassBillData data);
    }
    //    2.初始化点击时间
    public ClassBillAdapter.OnItemClickListener onItemClickListener;
    // 3. 定义构造函数方法，在主页面onCreate点击出发时传递回来，在再点击事件判断是否调用，有调用，把当前索引传递
    public void setOnItemClickListener(ClassBillAdapter.OnItemClickListener onMyItemClickListener) {
        onItemClickListener = onMyItemClickListener;
    }
}
